package SwordFingerOffer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Q32_PrintTreeLevel1 {
    //从上到下  横向打印二叉树

    public int[] levelOrder(TreeNode root) {
        if (root == null){
            return new int[]{};
        }
        //队列迭代
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        TreeNode poll = null;
        List<Integer> ans = new ArrayList<>();

        while (!queue.isEmpty()){
            poll = queue.poll();
            ans.add(poll.val);
            if (poll.left != null)
            queue.add(poll.left);
            if (poll.right != null)
            queue.add(poll.right);
        }

        int[] res = new int[ans.size()];
        for (int i = 0; i < ans.size(); i++) {
            res[i] = ans.get(i);
        }
        return res;

    }

}
